﻿using System;
using System.IO;
using IllusionUtility.GetUtility;
using UnityEngine;

// Token: 0x020002FE RID: 766
public abstract class PngAssist
{
	// Token: 0x060012BD RID: 4797 RVA: 0x00098040 File Offset: 0x00096240
	public static bool CheckPngData(Stream st, ref long size, bool skip)
	{
		if (st == null)
		{
			return false;
		}
		size = 0L;
		long position = st.Position;
		try
		{
			byte[] array = new byte[8];
			byte[] array2 = new byte[]
			{
				137,
				80,
				78,
				71,
				13,
				10,
				26,
				10
			};
			st.Read(array, 0, 8);
			for (int i = 0; i < 8; i++)
			{
				if (array[i] != array2[i])
				{
					st.Seek(position, SeekOrigin.Begin);
					return false;
				}
			}
			bool flag = true;
			while (flag)
			{
				byte[] array3 = new byte[4];
				st.Read(array3, 0, 4);
				Array.Reverse(array3);
				int num = BitConverter.ToInt32(array3, 0);
				byte[] array4 = new byte[4];
				st.Read(array4, 0, 4);
				int num2 = BitConverter.ToInt32(array4, 0);
				if (num2 == 1145980233)
				{
					flag = false;
				}
				if ((long)(num + 4) > st.Length - st.Position)
				{
					global::Debug.LogError("PNGが破損している可能性があります：");
					st.Seek(position, SeekOrigin.Begin);
					return false;
				}
				st.Seek((long)(num + 4), SeekOrigin.Current);
			}
			size = st.Position - position;
			if (!skip)
			{
				st.Seek(position, SeekOrigin.Begin);
			}
		}
		catch (EndOfStreamException ex)
		{
			global::Debug.LogError("PNGが破損している可能性があります：" + ex.GetType().Name);
			st.Seek(position, SeekOrigin.Begin);
			return false;
		}
		return true;
	}

	// Token: 0x060012BE RID: 4798 RVA: 0x000981C8 File Offset: 0x000963C8
	public static bool CheckPngData(BinaryReader reader, ref long size, bool skip)
	{
		return reader != null && global::PngAssist.CheckPngData(reader.BaseStream, ref size, skip);
	}

	// Token: 0x060012BF RID: 4799 RVA: 0x000981E0 File Offset: 0x000963E0
	public static bool CheckPngData(byte[] data, ref long size)
	{
		bool result;
		using (MemoryStream memoryStream = new MemoryStream())
		{
			memoryStream.Write(data, 0, data.Length);
			memoryStream.Seek(0L, SeekOrigin.Begin);
			result = global::PngAssist.CheckPngData(memoryStream, ref size, false);
		}
		return result;
	}

	// Token: 0x060012C0 RID: 4800 RVA: 0x0009823C File Offset: 0x0009643C
	public static byte[] LoadPngData(string fullpath)
	{
		byte[] result;
		using (FileStream fileStream = new FileStream(fullpath, FileMode.Open, FileAccess.Read))
		{
			using (BinaryReader binaryReader = new BinaryReader(fileStream))
			{
				result = global::PngAssist.LoadPngData(binaryReader);
			}
		}
		return result;
	}

	// Token: 0x060012C1 RID: 4801 RVA: 0x000982A8 File Offset: 0x000964A8
	public static byte[] LoadPngData(BinaryReader reader)
	{
		if (reader == null)
		{
			return null;
		}
		long num = 0L;
		global::PngAssist.CheckPngData(reader.BaseStream, ref num, false);
		if (num == 0L)
		{
			return null;
		}
		return reader.ReadBytes((int)num);
	}

	// Token: 0x060012C2 RID: 4802 RVA: 0x000982E0 File Offset: 0x000964E0
	public static Sprite LoadSpriteFromFile(string path, int width, int height, Vector2 pivot)
	{
		if (!File.Exists(path))
		{
			return null;
		}
		Sprite result;
		using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
		{
			using (BinaryReader binaryReader = new BinaryReader(fileStream))
			{
				byte[] data = binaryReader.ReadBytes((int)binaryReader.BaseStream.Length);
				Texture2D texture2D = new Texture2D(width, height);
				if (null == texture2D)
				{
					result = null;
				}
				else
				{
					texture2D.LoadImage(data);
					if (width == 0 || height == 0)
					{
						width = texture2D.width;
						height = texture2D.height;
					}
					result = Sprite.Create(texture2D, new Rect(0f, 0f, (float)width, (float)height), pivot);
				}
			}
		}
		return result;
	}

	// Token: 0x060012C3 RID: 4803 RVA: 0x000983C0 File Offset: 0x000965C0
	public static Sprite LoadSpriteFromAssetBundle(string assetBundleName, string assetName, int width, int height, Vector2 pivot)
	{
		TextAsset textAsset = global::CommonLib.LoadAsset<TextAsset>(assetBundleName, assetName, false, string.Empty);
		global::AssetBundleManager.UnloadAssetBundle(assetBundleName, true, null);
		Texture2D texture2D = new Texture2D(width, height);
		if (null == texture2D)
		{
			return null;
		}
		texture2D.LoadImage(textAsset.bytes);
		if (width == 0 || height == 0)
		{
			width = texture2D.width;
			height = texture2D.height;
		}
		return Sprite.Create(texture2D, new Rect(0f, 0f, (float)width, (float)height), pivot);
	}

	// Token: 0x060012C4 RID: 4804 RVA: 0x00098440 File Offset: 0x00096640
	public static Texture2D ChangeTextureFromByte(byte[] data, int width = 0, int height = 0)
	{
		Texture2D texture2D = new Texture2D(width, height);
		if (null == texture2D)
		{
			return null;
		}
		texture2D.LoadImage(data);
		return texture2D;
	}

	// Token: 0x060012C5 RID: 4805 RVA: 0x0009846C File Offset: 0x0009666C
	public static void SavePng(BinaryWriter writer, int createW = 252, int createH = 352, float renderRate = 1f)
	{
		byte[] array = null;
		if (createW == 0 || createH == 0)
		{
			createW = 252;
			createH = 352;
			global::Debug.LogWarning("作成する幅高さに０が入っている!");
		}
		global::PngAssist.CreatePng(ref array, createW, createH, renderRate);
		if (array == null)
		{
			return;
		}
		writer.Write(array);
		array = null;
	}

	// Token: 0x060012C6 RID: 4806 RVA: 0x000984BC File Offset: 0x000966BC
	public static void CreatePng(ref byte[] pngData, int createW = 252, int createH = 352, float renderRate = 1f)
	{
		if (createW == 0 || createH == 0)
		{
			createW = 252;
			createH = 352;
			global::Debug.LogWarning("作成する幅高さに０が入っている!");
		}
		GameObject gameObject = GameObject.FindGameObjectWithTag("SpriteTop");
		if (null == gameObject)
		{
			return;
		}
        Vector2 screenSize = Vector2.one;
		float screenRate = 1;
        float screenCorrectY = 1;
		float num = 720f * screenRate / screenSize.y;
		int num2 = 504;
		int num3 = 704;
		int num4 = (int)((float)num2 * renderRate);
		int num5 = (int)((float)num3 * renderRate);
		RenderTexture temporary;
		if (QualitySettings.antiAliasing == 0)
		{
			temporary = RenderTexture.GetTemporary((int)(1280f * renderRate / num), (int)(720f * renderRate / num), 24);
		}
		else
		{
			temporary = RenderTexture.GetTemporary((int)(1280f * renderRate / num), (int)(720f * renderRate / num), 24, RenderTextureFormat.Default, RenderTextureReadWrite.Default, QualitySettings.antiAliasing);
		}
		GameObject gameObject2 = gameObject.transform.FindLoop("BackSpCam");
		if (gameObject2)
		{
			Camera component = gameObject2.GetComponent<Camera>();
			if (component)
			{
				component.targetTexture = temporary;
				component.Render();
				component.targetTexture = null;
			}
		}
		if (Camera.main)
		{
			Camera main = Camera.main;
			RenderTexture targetTexture = main.targetTexture;
			Rect rect = main.rect;
			main.targetTexture = temporary;
			main.Render();
			main.targetTexture = targetTexture;
			main.rect = rect;
		}
		GameObject gameObject3 = gameObject.transform.FindLoop("FrontSpCam");
		if (gameObject3)
		{
			Camera component2 = gameObject3.GetComponent<Camera>();
			if (component2)
			{
				component2.targetTexture = temporary;
				component2.Render();
				component2.targetTexture = null;
			}
		}
		Texture2D texture2D = new Texture2D(num4, num5, TextureFormat.RGB24, false, true);
		RenderTexture.active = temporary;
		float x = 388f * renderRate + (1280f / num - 1280f) * 0.5f * renderRate;
		float y = 8f * renderRate + screenCorrectY / screenRate * renderRate;
		texture2D.ReadPixels(new Rect(x, y, (float)num4, (float)num5), 0, 0);
		texture2D.Apply();
		RenderTexture.active = null;
		RenderTexture.ReleaseTemporary(temporary);
		if (num4 != createW || num5 != createH)
		{
			global::TextureScale.Bilinear(texture2D, createW, createH);
		}
		pngData = texture2D.EncodeToPNG();
		UnityEngine.Object.Destroy(texture2D);
	}

	// Token: 0x060012C7 RID: 4807 RVA: 0x0009871C File Offset: 0x0009691C
	public static void CreatePngScreen(ref byte[] pngData, int createW, int createH)
	{
		Vector2 screenSize = Vector2.one;
		int num = (int)screenSize.x;
		int num2 = (int)screenSize.y;
		Texture2D texture2D = new Texture2D(num, num2, TextureFormat.RGB24, false);
		RenderTexture temporary;
		if (QualitySettings.antiAliasing == 0)
		{
			temporary = RenderTexture.GetTemporary(num, num2, 24);
		}
		else
		{
			temporary = RenderTexture.GetTemporary(num, num2, 24, RenderTextureFormat.Default, RenderTextureReadWrite.Default, QualitySettings.antiAliasing);
		}
		if (Camera.main)
		{
			Camera main = Camera.main;
			RenderTexture targetTexture = main.targetTexture;
			Rect rect = main.rect;
			main.targetTexture = temporary;
			main.Render();
			main.targetTexture = targetTexture;
			main.rect = rect;
		}
		RenderTexture.active = temporary;
		texture2D.ReadPixels(new Rect(0f, 0f, (float)num, (float)num2), 0, 0);
		texture2D.Apply();
		RenderTexture.active = null;
		RenderTexture.ReleaseTemporary(temporary);
		global::TextureScale.Bilinear(texture2D, createW, createH);
		pngData = texture2D.EncodeToPNG();
		UnityEngine.Object.Destroy(texture2D);
	}

	// Token: 0x060012C8 RID: 4808 RVA: 0x00098814 File Offset: 0x00096A14
	public static Sprite LoadSpriteFromFile(string path)
	{
		Sprite result;
		using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
		{
			long num = 0L;
			global::PngAssist.CheckPngData(fileStream, ref num, false);
			if (num == 0L)
			{
				result = null;
			}
			else
			{
				using (BinaryReader binaryReader = new BinaryReader(fileStream))
				{
					byte[] data = binaryReader.ReadBytes((int)num);
					int num2 = 0;
					int num3 = 0;
					Texture2D texture2D = global::PngAssist.ChangeTextureFromPngByte(data, ref num2, ref num3);
					if (null == texture2D)
					{
						result = null;
					}
					else
					{
						result = Sprite.Create(texture2D, new Rect(0f, 0f, (float)num2, (float)num3), new Vector2(0.5f, 0.5f));
					}
				}
			}
		}
		return result;
	}

	// Token: 0x060012C9 RID: 4809 RVA: 0x000988F0 File Offset: 0x00096AF0
	public static Texture2D ChangeTextureFromPngByte(byte[] data, ref int width, ref int height)
	{
		Texture2D texture2D = new Texture2D(width, height);
		if (null == texture2D)
		{
			return null;
		}
		texture2D.LoadImage(data);
		width = texture2D.width;
		height = texture2D.height;
		return texture2D;
	}

	// Token: 0x060012CA RID: 4810 RVA: 0x00098930 File Offset: 0x00096B30
	public static Texture2D LoadTexture(string _path)
	{
		Texture2D result;
		using (FileStream fileStream = new FileStream(_path, FileMode.Open, FileAccess.Read))
		{
			long num = 0L;
			global::PngAssist.CheckPngData(fileStream, ref num, false);
			if (num == 0L)
			{
				result = null;
			}
			else
			{
				using (BinaryReader binaryReader = new BinaryReader(fileStream))
				{
					byte[] data = binaryReader.ReadBytes((int)num);
					int num2 = 0;
					int num3 = 0;
					result = global::PngAssist.ChangeTextureFromPngByte(data, ref num2, ref num3);
				}
			}
		}
		return result;
	}
}
